{
 "cells":[
  {
   "cell_type":"code",
   "source":[
    "import numpy as np"
   ],
   "execution_count":20,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "def Gauss_CPE(A,b):\n",
    "    n=len(b);index=1;x=np.zeros(n)\n",
    "    for k in range(n):\n",
    "        a_max=0\n",
    "        for i in range(k,n):\n",
    "            if abs(A[i][k])>a_max:\n",
    "                a_max = abs(A[i][k])\n",
    "                r=i\n",
    "        if a_max<0.00000001:\n",
    "            index=0\n",
    "            return\n",
    "        if r>k:\n",
    "            for j in range(k,n):\n",
    "                z=A[k][j];A[k][j]=A[r][j];A[r][j]=z\n",
    "            z=b[k];b[k]=b[r];b[r]=z\n",
    "        for i in range(k+1,n):\n",
    "            m=A[i][k]\/A[k][k]\n",
    "            for j in range(k+1,n):\n",
    "                A[i][j]=A[i][j]-m*A[k][j]\n",
    "            b[i]=b[i]-m*b[k]\n",
    "    if abs(A[n-1][n-1])<0.0000001:\n",
    "        index=0\n",
    "        return\n",
    "    for k in range(n-1,0-1,-1):\n",
    "        for j in range(k+1,n):\n",
    "            b[k]=b[k]-A[k][j]*x[j]\n",
    "        x[k]=b[k]\/A[k][k]\n",
    "    return index, x"
   ],
   "execution_count":21,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "def Jacobi(A,b,x0,it_max,ep):\n",
    "    D=np.diag(np.diag(A))\n",
    "    U=-np.triu(A,1);L=-np.tril(A,-1)\n",
    "    B=np.dot (np.linalg.inv (D),(L+U))\n",
    "    f=np.dot (np.linalg.inv(D),b)\n",
    "    x=np.dot(B,x0)+f\n",
    "    k=1;index=1\n",
    "    while np.linalg.norm(x-x0)>=ep:\n",
    "        x0=x\n",
    "        x=np.dot(B,x0)+f\n",
    "        k=k+1\n",
    "        if k>it_max:\n",
    "            index=0;break\n",
    "    return k,index,x\n",
    "            "
   ],
   "execution_count":22,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "def G_S(A,b,x0,it_max,ep):\n",
    "    D=np.diag(np.diag(A))\n",
    "    U=-np.triu(A,1);L=-np.tril(A,-1)\n",
    "    B=np.dot (np.linalg.inv (D-L),U)\n",
    "    f=np.dot (np.linalg.inv(D-L),b)\n",
    "    x=np.dot(B,x0)+f\n",
    "    k=1;index=1\n",
    "    while np.linalg.norm(x-x0)>=ep:\n",
    "        x0=x\n",
    "        x=np.dot(B,x0)+f\n",
    "        k=k+1\n",
    "        if k>it_max:\n",
    "            index=0;break\n",
    "    return k,index,x"
   ],
   "execution_count":23,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "A=[[15,3,5,6],[2,15,6,5],[3,6,18,4],[2,5,3,13]]\n",
    "b=[22,28,13,15]\n",
    "x0=[0,0,0,0]"
   ],
   "execution_count":24,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "print(Gauss_CPE(A,b))\n",
    "print(Jacobi(A,b,x0,100,0.000001))\n",
    "print(G_S(A,b,x0,100,0.000001))"
   ],
   "execution_count":26,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  }
 ],
 "metadata":{
  "datalore":{
   "version":1,
   "computation_mode":"JUPYTER",
   "package_manager":"pip",
   "base_environment":"default",
   "packages":[
    
   ]
  }
 },
 "nbformat":4,
 "nbformat_minor":4
}